<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>Swift 备忘清单
 &#x26;  swift cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="该备忘单提供了使用 Swift 的示例，涵盖 Swift 基础知识、控制流、类型、结构/类、运算符、函数方法等。

入门，为开发人员分享快速参考备忘单。">
<meta keywords="swift,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="../style/style.css">
<link rel="stylesheet" href="../style/katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="../index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/swift.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js?v=1.5.2"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="swift-备忘清单"><svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" width="1em" height="1em" viewBox="0 0 150 150">
  <path d="M149.996652,41.5504118 C149.996652,40.0404009 149.969865,38.5370862 149.929684,37.0237271 C149.889479,33.7232863 149.600703,30.4305622 149.065807,27.1735006 C148.507382,23.9127729 147.466929,20.7532587 145.978615,17.7987099 C142.956518,11.8649909 138.1316,7.04156844 132.19675,4.0211156 C129.242865,2.53082012 126.082821,1.49033249 122.821331,0.934130935 C119.563242,0.396319522 116.269046,0.106431705 112.967097,0.066962791 C111.456985,0.0267851164 109.953569,0.0100444187 108.440109,0 L41.5565427,0 C40.0464306,0 38.5430153,0.0267851164 37.0295549,0.066962791 C36.2092058,0.0903997679 35.3855083,0.117184884 34.5618108,0.157362559 C32.0854761,0.256798914 29.6181641,0.516263128 27.1753203,0.934130935 C24.7417426,1.35877149 22.3608012,2.04348485 20.0734408,2.97649606 C19.3010849,3.30014955 18.5410063,3.64835606 17.7932051,4.0211156 C15.5863952,5.15055914 13.519722,6.53499141 11.6355641,8.14602353 C11.0060717,8.68507399 10.4000179,9.2442133 9.81740256,9.8200933 C7.47219368,12.1591083 5.51152349,14.8541431 4.00799143,17.8054061 C2.51889323,20.7596305 1.47840127,23.9192644 0.920800036,27.1801969 C0.390726031,30.4353902 0.106428217,33.7258611 0.0703156391,37.0237271 C0.0267869101,38.5337381 0.0100450913,40.0370527 0,41.5504118 L0,108.456284 C0,109.966295 0.0267869101,111.46961 0.0703156391,112.979621 C0.110229315,116.280076 0.39900748,119.572822 0.934193491,122.829848 C1.49106583,126.090961 2.53159273,129.250701 4.02138488,132.204638 C7.04518753,138.133686 11.8652306,142.95575 17.7932051,145.982233 C20.747852,147.46998 23.9076759,148.509267 27.1686236,149.065869 C30.4267126,149.60368 33.7209094,149.893568 37.0228582,149.933037 C38.5296219,149.973215 40.0363856,149.989956 41.549846,150 L108.446806,150 C109.956918,150 111.460333,149.973215 112.973793,149.933037 C116.276759,149.891708 119.571929,149.601833 122.831376,149.065869 C126.092204,148.508782 129.251957,147.469519 132.206795,145.982233 C138.136507,142.956565 142.95707,138.132809 145.978615,132.20129 C147.468407,129.247353 148.508934,126.087613 149.065807,122.826499 C149.603754,119.569766 149.893662,116.276895 149.933033,112.976273 C149.973213,111.46961 149.989955,109.962947 150,108.452936 L150,46.9241758 C149.996652,45.1329211 149.996652,43.3416665 149.996652,41.5504118 Z M126.598286,122.240575 C119.901558,109.206268 107.4356,112.440571 101.046922,115.721747 C100.471003,116.056561 99.8816911,116.391375 99.2890308,116.726189 L99.1483995,116.809893 C85.9223626,123.840986 68.1659896,124.356599 50.312514,116.682663 C35.779112,110.381458 23.4900254,99.8344883 15.0575919,86.4255262 C19.1601349,89.4606647 23.5534617,92.08168 28.1731327,94.2501283 C47.0445109,103.089217 66.0096433,102.449722 79.4030984,94.2501283 C60.3174249,79.5685364 44.3792134,60.4339189 32.161034,44.9588179 C29.8334165,42.2434232 27.7304143,39.3433109 25.8728068,36.2871364 C40.5051565,49.6796946 63.6088665,66.4940515 71.9061119,71.1479654 C54.3037636,52.5691391 38.7941426,29.6511239 39.5207375,30.3877146 C67.2418412,58.3178947 92.819992,74.1110689 92.819992,74.1110689 C93.7843207,74.640075 94.4941738,75.0652887 95.0734408,75.4503248 C95.6092689,74.0835027 96.0741937,72.6899392 96.4663601,71.2751948 C100.892897,55.0936363 95.9105317,36.5951653 84.6432876,21.3209527 C110.355373,36.7491797 125.553596,66.075534 119.32564,90.8785518 C119.178312,91.45778 119.007545,92.0169193 118.843475,92.5827548 C131.741372,108.452936 128.232287,125.451441 126.588241,122.243923 L126.598286,122.240575 Z"></path>
</svg><a aria-hidden="true" tabindex="-1" href="#swift-备忘清单"><span class="icon icon-link"></span></a>Swift 备忘清单</h1><div class="wrap-body">
<p>该备忘单提供了使用 <a href="https://www.swift.org">Swift</a> 的示例，涵盖 Swift 基础知识、控制流、类型、结构/类、运算符、函数方法等。</p>
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#变量">变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#类型注释">类型注释</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#算术运算符">算术运算符</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#复合赋值运算符">复合赋值运算符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字符串插值">字符串插值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#多行字符串">多行字符串</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#代码注释">代码注释</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#组成一个元组">组成一个元组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#分解元组">分解元组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#特殊注释语法-mark">特殊注释语法 (MARK)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#特殊注释语法-todo">特殊注释语法 (TODO)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#特殊注释语法-fixme">特殊注释语法 (FIXME)</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#变量-1">变量</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#变量声明">变量声明</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#常数">常数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#计算变量get-和-set">计算变量（get 和 set）</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#willset">willSet</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#didset">didSet</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#willset-和-didset">willSet 和 didSet</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#条件和逻辑">条件和逻辑</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#if-语句">if 语句</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#else-语句">else 语句</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#else-if-语句">else if 语句</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#比较运算符">比较运算符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#三元条件运算符">三元条件运算符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#switch-语句">switch 语句</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#switch-语句区间匹配">switch 语句：区间匹配</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#switch-语句复合案例">switch 语句：复合案例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#switch-语句where-子句">switch 语句：where 子句</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#逻辑运算符">逻辑运算符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#逻辑运算符-">逻辑运算符 &#x26;&#x26;</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#逻辑运算符--1">逻辑运算符 ||</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#组合逻辑运算符">组合逻辑运算符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#控制执行顺序">控制执行顺序</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#简单的-guard">简单的 guard</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#循环">循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#范围">范围</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#stride-函数">stride() 函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#for-in-循环">for-in 循环</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#continue-关键字">continue 关键字</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#break-关键字">break 关键字</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用下划线">使用下划线</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#while-循环">while 循环</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#数组和集合">数组和集合</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#array-数组">Array 数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#count-属性">.count 属性</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#索引">索引</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#用数组字面量初始化">用数组字面量初始化</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#append-方法和--运算符">.append() 方法和 += 运算符</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#insert-和-remove-方法">.insert() 和 .remove() 方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#遍历数组">遍历数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#集合set">集合(Set)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#空集合set">空集合(Set)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#填充集合">填充集合</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#insert">.insert()</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#remove-和-removeall-方法">.remove() 和 .removeAll() 方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#contains">.contains()</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#迭代一个集合">迭代一个集合</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#isempty-属性">.isEmpty 属性</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#count-属性-1">.count 属性</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#intersection-交叉">.intersection() 交叉</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#union-合并去重">.union() 合并去重</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#symmetricdifference-对称差">.symmetricDifference() 对称差</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#subtracting-减法">.subtracting() 减法</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#字典">字典</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#基础字典">基础字典</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#keys">Keys</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#类型一致性">类型一致性</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#初始化填充字典">初始化填充字典</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#初始化一个空字典">初始化一个空字典</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#添加到字典">添加到字典</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#删除键值对">删除键值对</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#修改键值对">修改键值对</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#isempty-属性-1">.isEmpty 属性</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#count-属性-2">.count 属性</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#为变量赋值">为变量赋值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#遍历字典">遍历字典</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#函数">函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#基础函数">基础函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#调用函数">调用函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#返回值">返回值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#多个参数">多个参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#省略参数标签">省略参数标签</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#返回多个值">返回多个值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#parameters--arguments">Parameters &#x26; Arguments</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#隐式返回">隐式返回</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#默认参数">默认参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#输入输出参数">输入输出参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#可变参数">可变参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#可选参数">可选参数</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#结构">结构</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#结构创建">结构创建</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#默认属性值">默认属性值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#结构实例创建">结构实例创建</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#init-方法">init() 方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#检查类型">检查类型</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#变异方法mutating">变异方法(mutating)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#结构方法">结构方法</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#class">Class</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#引用类型类">引用类型（类）</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#类的实例">类的实例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#init-方法-1">init() 方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#类属性">类属性</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#继承">继承</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#示例">示例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#这是结构定义和类定义的示例">这是结构定义和类定义的示例</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#枚举">枚举</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#定义枚举">定义枚举</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#switch-语句-1">Switch 语句</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#caseiterable">CaseIterable</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#原始值">原始值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#相关值">相关值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#实例方法">实例方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#从原始值初始化">从原始值初始化</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#计算属性">计算属性</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="变量"><a aria-hidden="true" tabindex="-1" href="#变量"><span class="icon icon-link"></span></a>变量</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> score <span class="token operator">=</span> <span class="token number">0</span> <span class="token comment">// 变量</span>
</span><span class="code-line"><span class="token keyword">let</span> pi <span class="token operator">=</span> <span class="token number">3.14</span> <span class="token comment">// 常数</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">var</span> greeting <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Hello"</span></span>
</span><span class="code-line"><span class="token keyword">var</span> numberOfToys <span class="token operator">=</span> <span class="token number">8</span>
</span><span class="code-line"><span class="token keyword">var</span> isMorning <span class="token operator">=</span> <span class="token boolean">true</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">var</span> numberOfToys<span class="token punctuation">:</span> <span class="token class-name">Int</span> <span class="token operator">=</span> <span class="token number">8</span>
</span><span class="code-line">numberOfToys <span class="token operator">+=</span> <span class="token number">1</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>numberOfToys<span class="token punctuation">)</span> <span class="token comment">// 打印 "9"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="类型注释"><a aria-hidden="true" tabindex="-1" href="#类型注释"><span class="icon icon-link"></span></a>类型注释</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> greeting<span class="token punctuation">:</span> <span class="token class-name">String</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Hello"</span></span>
</span><span class="code-line"><span class="token keyword">var</span> numberOfToys<span class="token punctuation">:</span> <span class="token class-name">Int</span> <span class="token operator">=</span> <span class="token number">8</span>
</span><span class="code-line"><span class="token keyword">var</span> isMorning<span class="token punctuation">:</span> <span class="token class-name">Bool</span> <span class="token operator">=</span> <span class="token boolean">true</span>
</span><span class="code-line"><span class="token keyword">var</span> price<span class="token punctuation">:</span> <span class="token class-name">Double</span> <span class="token operator">=</span> <span class="token number">8.99</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="算术运算符"><a aria-hidden="true" tabindex="-1" href="#算术运算符"><span class="icon icon-link"></span></a>算术运算符</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<ul class="cols-5 style-none">
<li><code>+</code> 添加</li>
<li><code>-</code> 减法</li>
<li><code>*</code> 乘法</li>
<li><code>/</code> 分配</li>
<li><code>%</code> 余数</li>
</ul>
<!--rehype:className=cols-5 style-none-->
<hr>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> x <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line">x <span class="token operator">=</span> <span class="token number">4</span> <span class="token operator">+</span> <span class="token number">2</span>  <span class="token comment">// x 现在是 6</span>
</span><span class="code-line">x <span class="token operator">=</span> <span class="token number">4</span> <span class="token operator">-</span> <span class="token number">2</span>  <span class="token comment">// x 现在是 2</span>
</span><span class="code-line">x <span class="token operator">=</span> <span class="token number">4</span> <span class="token operator">*</span> <span class="token number">2</span>  <span class="token comment">// x 现在是 8</span>
</span><span class="code-line">x <span class="token operator">=</span> <span class="token number">4</span> <span class="token operator">/</span> <span class="token number">2</span>  <span class="token comment">// x 现在是 2</span>
</span><span class="code-line">x <span class="token operator">=</span> <span class="token number">4</span> <span class="token operator">%</span> <span class="token number">2</span>  <span class="token comment">// x 现在是 0</span>
</span></code></pre>
<h4 id="复合赋值运算符"><a aria-hidden="true" tabindex="-1" href="#复合赋值运算符"><span class="icon icon-link"></span></a>复合赋值运算符</h4>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> numberOfDogs <span class="token operator">=</span> <span class="token number">100</span>
</span><span class="code-line">numberOfDogs <span class="token operator">+=</span> <span class="token number">1</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"有 </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">numberOfDogs</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string"> 个斑点狗！"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 打印: 有 101 个斑点狗！</span>
</span></code></pre>
<hr>
<ul class="style-arrow cols-2">
<li><code>+=</code> 添加和分配总和</li>
<li><code>-=</code> 减去并分配差值</li>
<li><code>*=</code> 乘并赋值</li>
<li><code>/=</code> 除并分配商</li>
<li><code>%=</code> 除并分配余数</li>
</ul>
<!--rehype:className=style-arrow cols-2-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="字符串插值"><a aria-hidden="true" tabindex="-1" href="#字符串插值"><span class="icon icon-link"></span></a>字符串插值</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> apples <span class="token operator">=</span> <span class="token number">6</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"I have </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">apples</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string"> apples!"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 打印: I have 6 apples!</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="多行字符串"><a aria-hidden="true" tabindex="-1" href="#多行字符串"><span class="icon icon-link"></span></a>多行字符串</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">let</span> myLongString <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"""
</span></span></span><span class="code-line"><span class="token string-literal"><span class="token string">Swift?
</span></span></span><span class="code-line"><span class="token string-literal"><span class="token string">这是我最喜欢的语言！
</span></span></span><span class="code-line"><span class="token string-literal"><span class="token string">"""</span></span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="代码注释"><a aria-hidden="true" tabindex="-1" href="#代码注释"><span class="icon icon-link"></span></a>代码注释</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token comment">// 这一行表示 Swift 中的注释。</span>
</span><span class="code-line"><span class="token comment">/*
</span></span><span class="code-line"><span class="token comment">这都被注释掉了。
</span></span><span class="code-line"><span class="token comment">没有一个会跑！
</span></span><span class="code-line"><span class="token comment">*/</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="组成一个元组"><a aria-hidden="true" tabindex="-1" href="#组成一个元组"><span class="icon icon-link"></span></a>组成一个元组</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">let</span> player <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"Maya"</span></span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">150</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>player<span class="token punctuation">)</span> <span class="token comment">// ("Maya", 5, 150)</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"</span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">player<span class="token punctuation">.</span><span class="token number">0</span></span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">: level </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">player<span class="token punctuation">.</span><span class="token number">1</span></span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">, </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">player<span class="token punctuation">.</span><span class="token number">2</span></span><span class="token interpolation-punctuation punctuation">)</span><span class="token string"> pts"</span></span><span class="token punctuation">)</span> <span class="token comment">// Maya: level 5, 150 pts</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="分解元组"><a aria-hidden="true" tabindex="-1" href="#分解元组"><span class="icon icon-link"></span></a>分解元组</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">let</span> player <span class="token operator">=</span> <span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"Maya"</span></span><span class="token punctuation">,</span> level<span class="token punctuation">:</span> <span class="token number">5</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">let</span> <span class="token punctuation">(</span>currentName<span class="token punctuation">,</span> curLevel<span class="token punctuation">)</span> <span class="token operator">=</span> player
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"</span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">currentName</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">: level </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">curLevel</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 打印: Maya: level 5</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="特殊注释语法-mark"><a aria-hidden="true" tabindex="-1" href="#特殊注释语法-mark"><span class="icon icon-link"></span></a>特殊注释语法 (MARK)</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token comment">// MARK: - 查看设置</span>
</span></code></pre>
<p><code>MARK</code> 可用于在栏中显示评论</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="特殊注释语法-todo"><a aria-hidden="true" tabindex="-1" href="#特殊注释语法-todo"><span class="icon icon-link"></span></a>特殊注释语法 (TODO)</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token comment">// TODO: 更新逻辑以适应数据更改</span>
</span></code></pre>
<p><code>TODO</code> 用于显示需要完成的事情的提醒</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="特殊注释语法-fixme"><a aria-hidden="true" tabindex="-1" href="#特殊注释语法-fixme"><span class="icon icon-link"></span></a>特殊注释语法 (FIXME)</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token comment">// FIXME: 修复对现有条目进行更改时的故障行为</span>
</span></code></pre>
<p><code>FIXME</code> 用于显示需要修复的内容的提醒</p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="变量-1"><a aria-hidden="true" tabindex="-1" href="#变量-1"><span class="icon icon-link"></span></a>变量</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="变量声明"><a aria-hidden="true" tabindex="-1" href="#变量声明"><span class="icon icon-link"></span></a>变量声明</h3><div class="wrap-body">
<p>变量用 <code>var</code> 声明：</p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> greeting <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"你好"</span></span>
</span><span class="code-line"><span class="token keyword">var</span> numberOfToys <span class="token operator">=</span> <span class="token number">8</span>
</span><span class="code-line"><span class="token keyword">var</span> isMorning <span class="token operator">=</span> <span class="token boolean">true</span>
</span></code></pre>
<p>为了清楚起见，变量声明可以包含类型注释：</p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> greeting<span class="token punctuation">:</span> <span class="token class-name">String</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"你好"</span></span>
</span><span class="code-line"><span class="token keyword">var</span> numberOfToys<span class="token punctuation">:</span> <span class="token class-name">Int</span> <span class="token operator">=</span> <span class="token number">8</span>
</span><span class="code-line"><span class="token keyword">var</span> isMorning<span class="token punctuation">:</span> <span class="token class-name">Bool</span> <span class="token operator">=</span> <span class="token boolean">true</span>
</span></code></pre>
<p>变量是可变的。 它们的值可以改变：</p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> numberOfToys<span class="token punctuation">:</span> <span class="token class-name">Int</span> <span class="token operator">=</span> <span class="token number">8</span>
</span><span class="code-line">numberOfToys <span class="token operator">+=</span> <span class="token number">1</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>numberOfToys<span class="token punctuation">)</span> <span class="token comment">// 打印“9”</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="常数"><a aria-hidden="true" tabindex="-1" href="#常数"><span class="icon icon-link"></span></a>常数</h3><div class="wrap-body">
<p>常量用 <code>let</code> 声明：</p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">let</span> greeting <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Hello"</span></span>
</span><span class="code-line"><span class="token keyword">let</span> numberOfToys <span class="token operator">=</span> <span class="token number">8</span>
</span><span class="code-line"><span class="token keyword">let</span> isMorning <span class="token operator">=</span> <span class="token boolean">true</span>
</span></code></pre>
<p>为清楚起见，常量声明可以包含类型注释：</p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">let</span> greeting<span class="token punctuation">:</span> <span class="token class-name">String</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Hello"</span></span>
</span><span class="code-line"><span class="token keyword">let</span> numberOfToys<span class="token punctuation">:</span> <span class="token class-name">Int</span> <span class="token operator">=</span> <span class="token number">8</span>
</span><span class="code-line"><span class="token keyword">let</span> isMorning<span class="token punctuation">:</span> <span class="token class-name">Bool</span> <span class="token operator">=</span> <span class="token boolean">true</span>
</span></code></pre>
<p>常量是不可变的。它们的值不能改变：</p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">let</span> numberOfToys<span class="token punctuation">:</span> <span class="token class-name">Int</span> <span class="token operator">=</span> <span class="token number">8</span>
</span><span class="code-line">numberOfToys <span class="token operator">+=</span> <span class="token number">1</span>
</span><span class="code-line"><span class="token comment">// ❌ 错误：numberOfToys 不可变</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="计算变量get-和-set"><a aria-hidden="true" tabindex="-1" href="#计算变量get-和-set"><span class="icon icon-link"></span></a>计算变量（get 和 set）</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">import</span> <span class="token class-name">Foundation</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">let</span> df <span class="token operator">=</span> <span class="token class-name">DateFormatter</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">df<span class="token punctuation">.</span>dateFormat <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"d MMMM yyyy"</span></span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">var</span> birth <span class="token operator">=</span> df<span class="token punctuation">.</span><span class="token function">date</span><span class="token punctuation">(</span>from<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"5 June 1999"</span></span><span class="token punctuation">)</span><span class="token operator">!</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">var</span> age<span class="token punctuation">:</span> <span class="token class-name">Int</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token class-name">Calendar</span><span class="token punctuation">.</span>current
</span><span class="code-line">    <span class="token punctuation">.</span><span class="token function">dateComponents</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">.</span>year<span class="token punctuation">]</span><span class="token punctuation">,</span>
</span><span class="code-line">                  from<span class="token punctuation">:</span> birth<span class="token punctuation">,</span>
</span><span class="code-line">                  to<span class="token punctuation">:</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>year<span class="token operator">!</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span> <span class="token comment">// 20</span>
</span><span class="code-line">birth <span class="token operator">=</span> df<span class="token punctuation">.</span><span class="token function">date</span><span class="token punctuation">(</span>from<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"5 June 2002"</span></span><span class="token punctuation">)</span><span class="token operator">!</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>age<span class="token punctuation">)</span> <span class="token comment">// 17</span>
</span></code></pre>
<p>在下面的示例中，distanceInFeet 有一个 <code>getter</code> 和一个 <code>setter</code>。 因为有 <code>setter</code>，所以 <code>getter</code> 需要关键字 <code>get</code>：</p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> distanceInMeters<span class="token punctuation">:</span> <span class="token class-name">Float</span> <span class="token operator">=</span> <span class="token number">100</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">var</span> distanceInFeet<span class="token punctuation">:</span> <span class="token class-name">Float</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">get</span> <span class="token punctuation">{</span>
</span><span class="code-line">    distanceInMeters <span class="token operator">*</span> <span class="token number">3.28</span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line">  <span class="token keyword">set</span><span class="token punctuation">(</span>newDistance<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    distanceInMeters <span class="token operator">=</span> newDistance <span class="token operator">/</span> <span class="token number">3.28</span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>distanceInMeters<span class="token punctuation">)</span> <span class="token comment">// 100.0</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>distanceInFeet<span class="token punctuation">)</span> <span class="token comment">// 328.0</span>
</span><span class="code-line">
</span><span class="code-line">distanceInFeet <span class="token operator">=</span> <span class="token number">250</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>distanceInMeters<span class="token punctuation">)</span> <span class="token comment">// 76.21951</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>distanceInFeet<span class="token punctuation">)</span> <span class="token comment">// 250.0</span>
</span><span class="code-line">
</span><span class="code-line">distanceInMeters <span class="token operator">=</span> <span class="token number">800</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>distanceInMeters<span class="token punctuation">)</span> <span class="token comment">// 800.0</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>distanceInFeet<span class="token punctuation">)</span> <span class="token comment">// 2624.0</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="willset"><a aria-hidden="true" tabindex="-1" href="#willset"><span class="icon icon-link"></span></a>willSet</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> distance <span class="token operator">=</span> <span class="token number">5</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">willSet</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"距离将被设置"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line">distance <span class="token operator">=</span> <span class="token number">10</span> <span class="token comment">// 打印: 距离将被设置</span>
</span></code></pre>
<p>可以在 <code>willSet</code> 中访问新值：</p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> distance <span class="token operator">=</span> <span class="token number">5</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">willSet</span><span class="token punctuation">(</span>newDistance<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"距离将被设置 </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">newDistance</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line">distance <span class="token operator">=</span> <span class="token number">10</span> <span class="token comment">// 打印: 距离将被设置 10</span>
</span></code></pre>
<p><code>willSet</code> 可用于在设置变量值之前执行一些代码</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="didset"><a aria-hidden="true" tabindex="-1" href="#didset"><span class="icon icon-link"></span></a>didSet</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> distance <span class="token operator">=</span> <span class="token number">5</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">didSet</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"距离设置为 </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">distance</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"它的旧值是： </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">oldValue</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">distance <span class="token operator">=</span> <span class="token number">10</span> <span class="token comment">// 打印: 距离将被设置 10</span>
</span><span class="code-line">              <span class="token comment">// 打印: 它的旧值是：5</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="willset-和-didset"><a aria-hidden="true" tabindex="-1" href="#willset-和-didset"><span class="icon icon-link"></span></a>willSet 和 didSet</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> distance <span class="token operator">=</span> <span class="token number">5</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">willSet</span><span class="token punctuation">(</span>newDistance<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"距离将设置为 </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">newDistance</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line">  <span class="token keyword">didSet</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"距离设置为 </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">distance</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"它的旧值是： </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">oldValue</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">distance <span class="token operator">=</span> <span class="token number">10</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="条件和逻辑"><a aria-hidden="true" tabindex="-1" href="#条件和逻辑"><span class="icon icon-link"></span></a>条件和逻辑</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="if-语句"><a aria-hidden="true" tabindex="-1" href="#if-语句"><span class="icon icon-link"></span></a>if 语句</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> halloween <span class="token operator">=</span> <span class="token boolean">true</span> 
</span><span class="code-line"><span class="token keyword">if</span> halloween <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"不给糖就捣蛋！"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 打印: 不给糖就捣蛋！ </span>
</span><span class="code-line"><span class="token keyword">if</span> <span class="token number">5</span> <span class="token operator">></span> <span class="token number">3</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"5 大于 3"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"5 不超过 3"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 输出: "5 大于 3"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="else-语句"><a aria-hidden="true" tabindex="-1" href="#else-语句"><span class="icon icon-link"></span></a>else 语句</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> turbulence <span class="token operator">=</span> <span class="token boolean">false</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">if</span> turbulence <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"请坐好。"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"你可以自由地四处走动。"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 打印: 你可以自由地四处走动。</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="else-if-语句"><a aria-hidden="true" tabindex="-1" href="#else-if-语句"><span class="icon icon-link"></span></a>else if 语句</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> weather <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"rainy"</span></span>
</span><span class="code-line"><span class="token keyword">if</span> weather <span class="token operator">==</span> <span class="token string-literal"><span class="token string">"sunny"</span></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"拿点防晒霜"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> weather <span class="token operator">==</span> <span class="token string-literal"><span class="token string">"rainy"</span></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"拿一把雨伞"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> weather <span class="token operator">==</span> <span class="token string-literal"><span class="token string">"snowing"</span></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"穿上你的雪地靴"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"无效天气"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 打印: 拿一把雨伞</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="比较运算符"><a aria-hidden="true" tabindex="-1" href="#比较运算符"><span class="icon icon-link"></span></a>比较运算符</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token number">5</span> <span class="token operator">></span> <span class="token number">1</span>          <span class="token comment">// true </span>
</span><span class="code-line"><span class="token number">6</span> <span class="token operator">&#x3C;</span> <span class="token number">10</span>         <span class="token comment">// true </span>
</span><span class="code-line"><span class="token number">2</span> <span class="token operator">>=</span> <span class="token number">3</span>         <span class="token comment">// false</span>
</span><span class="code-line"><span class="token number">3</span> <span class="token operator">&#x3C;=</span> <span class="token number">5</span>         <span class="token comment">// true</span>
</span><span class="code-line"><span class="token string-literal"><span class="token string">"A"</span></span> <span class="token operator">==</span> <span class="token string-literal"><span class="token string">"a"</span></span>     <span class="token comment">// false</span>
</span><span class="code-line"><span class="token string-literal"><span class="token string">"B"</span></span> <span class="token operator">!=</span> <span class="token string-literal"><span class="token string">"b"</span></span>     <span class="token comment">// true</span>
</span></code></pre>
<ul class="style-round cols-2">
<li><code>&#x3C;</code> 小于</li>
<li><code>></code> 大于</li>
<li><code>&#x3C;=</code> 小于或等于</li>
<li><code>>=</code> 大于或等于</li>
<li><code>==</code> 等于</li>
<li><code>!=</code> 不等于</li>
</ul>
<!--rehype:className=style-round cols-2-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="三元条件运算符"><a aria-hidden="true" tabindex="-1" href="#三元条件运算符"><span class="icon icon-link"></span></a>三元条件运算符</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> driverLicense <span class="token operator">=</span> <span class="token boolean">true</span>
</span><span class="code-line">
</span><span class="code-line">driverLicense 
</span><span class="code-line">    <span class="token operator">?</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"驾驶座"</span></span><span class="token punctuation">)</span> <span class="token punctuation">:</span> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"乘客座位"</span></span><span class="token punctuation">)</span> 
</span><span class="code-line"><span class="token comment">// 打印: 驾驶座</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="switch-语句"><a aria-hidden="true" tabindex="-1" href="#switch-语句"><span class="icon icon-link"></span></a>switch 语句</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> secondaryColor <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"green"</span></span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">switch</span> secondaryColor <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token string-literal"><span class="token string">"orange"</span></span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"红色和黄色的混合"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token string-literal"><span class="token string">"purple"</span></span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"红色和蓝色的混合"</span></span><span class="token punctuation">)</span> 
</span><span class="code-line">  <span class="token keyword">default</span><span class="token punctuation">:</span> 
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"这可能不是辅助颜色"</span></span><span class="token punctuation">)</span> 
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 打印: 蓝色和黄色的混合</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="switch-语句区间匹配"><a aria-hidden="true" tabindex="-1" href="#switch-语句区间匹配"><span class="icon icon-link"></span></a>switch 语句：区间匹配</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">let</span> year <span class="token operator">=</span> <span class="token number">1905</span>
</span><span class="code-line"><span class="token keyword">var</span> artPeriod<span class="token punctuation">:</span> <span class="token class-name">String</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">switch</span> year <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token number">1860</span><span class="token operator">...</span><span class="token number">1885</span><span class="token punctuation">:</span>
</span><span class="code-line">    artPeriod <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"印象派"</span></span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token number">1886</span><span class="token operator">...</span><span class="token number">1910</span><span class="token punctuation">:</span>
</span><span class="code-line">    artPeriod <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"后印象派"</span></span>
</span><span class="code-line">  <span class="token keyword">default</span><span class="token punctuation">:</span>  
</span><span class="code-line">    artPeriod <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"未知"</span></span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 打印: 后印象派</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="switch-语句复合案例"><a aria-hidden="true" tabindex="-1" href="#switch-语句复合案例"><span class="icon icon-link"></span></a>switch 语句：复合案例</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">let</span> service <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Seamless"</span></span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">switch</span> service <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token string-literal"><span class="token string">"Uber"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Lyft"</span></span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"旅行"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token string-literal"><span class="token string">"DoorDash"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Seamless"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"GrubHub"</span></span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"餐厅送餐"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token string-literal"><span class="token string">"Instacart"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"FreshDirect"</span></span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"杂货配送"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">default</span><span class="token punctuation">:</span> 
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"未知服务"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 打印: 餐厅外卖</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="switch-语句where-子句"><a aria-hidden="true" tabindex="-1" href="#switch-语句where-子句"><span class="icon icon-link"></span></a>switch 语句：where 子句</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">let</span> num <span class="token operator">=</span> <span class="token number">7</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">switch</span> num <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token keyword">let</span> x <span class="token keyword">where</span> x <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"</span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">num</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string"> 是偶数"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token keyword">let</span> x <span class="token keyword">where</span> x <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"</span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">num</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string"> 奇数"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">default</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"</span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">num</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string"> 无效"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 打印: 7 奇数</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="逻辑运算符"><a aria-hidden="true" tabindex="-1" href="#逻辑运算符"><span class="icon icon-link"></span></a>逻辑运算符</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token operator">!</span><span class="token boolean">true</span>          <span class="token comment">// false</span>
</span><span class="code-line"><span class="token operator">!</span><span class="token boolean">false</span>         <span class="token comment">// true</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="逻辑运算符-"><a aria-hidden="true" tabindex="-1" href="#逻辑运算符-"><span class="icon icon-link"></span></a>逻辑运算符 &#x26;&#x26;</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token boolean">true</span> <span class="token operator">&#x26;&#x26;</span> <span class="token boolean">true</span>    <span class="token comment">// true</span>
</span><span class="code-line"><span class="token boolean">true</span> <span class="token operator">&#x26;&#x26;</span> <span class="token boolean">false</span>   <span class="token comment">// false </span>
</span><span class="code-line"><span class="token boolean">false</span> <span class="token operator">&#x26;&#x26;</span> <span class="token boolean">true</span>   <span class="token comment">// false </span>
</span><span class="code-line"><span class="token boolean">false</span> <span class="token operator">&#x26;&#x26;</span> <span class="token boolean">false</span>  <span class="token comment">// false</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="逻辑运算符--1"><a aria-hidden="true" tabindex="-1" href="#逻辑运算符--1"><span class="icon icon-link"></span></a>逻辑运算符 ||</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token boolean">true</span> <span class="token operator">||</span> <span class="token boolean">true</span>    <span class="token comment">// true</span>
</span><span class="code-line"><span class="token boolean">true</span> <span class="token operator">||</span> <span class="token boolean">false</span>   <span class="token comment">// true</span>
</span><span class="code-line"><span class="token boolean">false</span> <span class="token operator">||</span> <span class="token boolean">true</span>   <span class="token comment">// true </span>
</span><span class="code-line"><span class="token boolean">false</span> <span class="token operator">||</span> <span class="token boolean">false</span>  <span class="token comment">// false</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="组合逻辑运算符"><a aria-hidden="true" tabindex="-1" href="#组合逻辑运算符"><span class="icon icon-link"></span></a>组合逻辑运算符</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token operator">!</span><span class="token boolean">false</span> <span class="token operator">&#x26;&#x26;</span> <span class="token boolean">true</span> <span class="token operator">||</span> <span class="token boolean">false</span> <span class="token comment">// true</span>
</span></code></pre>
<p><code>!false &#x26;&#x26; true</code> 首先计算并返回 <code>true</code> 然后，表达式，<code>true</code> || <code>false</code> 评估并返回最终结果 <code>true</code></p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token boolean">false</span> <span class="token operator">||</span> <span class="token boolean">true</span> <span class="token operator">&#x26;&#x26;</span> <span class="token boolean">false</span>  <span class="token comment">// false</span>
</span></code></pre>
<p><code>true &#x26;&#x26; false</code> 首先计算返回 <code>false</code> 然后，表达式，<code>false</code> || <code>false</code> 评估并返回最终结果 <code>false</code></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="控制执行顺序"><a aria-hidden="true" tabindex="-1" href="#控制执行顺序"><span class="icon icon-link"></span></a>控制执行顺序</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token comment">// 没有括号：</span>
</span><span class="code-line"><span class="token boolean">true</span> <span class="token operator">||</span> <span class="token boolean">true</span> <span class="token operator">&#x26;&#x26;</span> <span class="token boolean">false</span> <span class="token operator">||</span> <span class="token boolean">false</span>
</span><span class="code-line"><span class="token comment">// ---->  true </span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 带括号：</span>
</span><span class="code-line"><span class="token punctuation">(</span><span class="token boolean">true</span> <span class="token operator">||</span> <span class="token boolean">true</span><span class="token punctuation">)</span> <span class="token operator">&#x26;&#x26;</span> <span class="token punctuation">(</span><span class="token boolean">false</span> <span class="token operator">||</span> <span class="token boolean">false</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// ---->  false </span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="简单的-guard"><a aria-hidden="true" tabindex="-1" href="#简单的-guard"><span class="icon icon-link"></span></a>简单的 guard</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">func</span> <span class="token function-definition function">greet</span><span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token operator">?</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">guard</span> <span class="token keyword">let</span> unwrapped <span class="token operator">=</span> name <span class="token keyword">else</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"Hello guest!"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token keyword">return</span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"Hello </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">unwrapped</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">!"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token function">greet</span><span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"Asma"</span></span><span class="token punctuation">)</span> <span class="token comment">// 输出：Hello Asma!</span>
</span><span class="code-line"><span class="token function">greet</span><span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token nil constant">nil</span><span class="token punctuation">)</span>    <span class="token comment">// 输出：Hello guest!</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="循环"><a aria-hidden="true" tabindex="-1" href="#循环"><span class="icon icon-link"></span></a>循环</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="范围"><a aria-hidden="true" tabindex="-1" href="#范围"><span class="icon icon-link"></span></a>范围</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">let</span> zeroToThree <span class="token operator">=</span> <span class="token number">0</span><span class="token operator">...</span><span class="token number">3</span>
</span><span class="code-line"><span class="token comment">// zeroToThree: 0, 1, 2, 3</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="stride-函数"><a aria-hidden="true" tabindex="-1" href="#stride-函数"><span class="icon icon-link"></span></a>stride() 函数</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">for</span> oddNum <span class="token keyword">in</span> <span class="token function">stride</span><span class="token punctuation">(</span>from<span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span> to<span class="token punctuation">:</span> <span class="token number">5</span><span class="token punctuation">,</span> by<span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span>oddNum<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 打印: 1</span>
</span><span class="code-line"><span class="token comment">// 打印: 3</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="for-in-循环"><a aria-hidden="true" tabindex="-1" href="#for-in-循环"><span class="icon icon-link"></span></a>for-in 循环</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">for</span> char <span class="token keyword">in</span> <span class="token string-literal"><span class="token string">"hehe"</span></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span>char<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 打印: h</span>
</span><span class="code-line"><span class="token comment">// 打印: e</span>
</span><span class="code-line"><span class="token comment">// 打印: h</span>
</span><span class="code-line"><span class="token comment">// 打印: e</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="continue-关键字"><a aria-hidden="true" tabindex="-1" href="#continue-关键字"><span class="icon icon-link"></span></a>continue 关键字</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">for</span> num <span class="token keyword">in</span> <span class="token number">0</span><span class="token operator">...</span><span class="token number">5</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">if</span> num <span class="token operator">%</span> <span class="token number">2</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">continue</span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 打印: 1</span>
</span><span class="code-line"><span class="token comment">// 打印: 3</span>
</span><span class="code-line"><span class="token comment">// 打印: 5</span>
</span></code></pre>
<p><code>continue</code> 关键字将强制循环继续进行下一次迭代</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="break-关键字"><a aria-hidden="true" tabindex="-1" href="#break-关键字"><span class="icon icon-link"></span></a>break 关键字</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">for</span> char <span class="token keyword">in</span> <span class="token string-literal"><span class="token string">"supercalifragilistice"</span></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">if</span> char <span class="token operator">==</span> <span class="token string-literal"><span class="token string">"c"</span></span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">break</span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span>char<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 打印: s</span>
</span><span class="code-line"><span class="token comment">// 打印: u</span>
</span><span class="code-line"><span class="token comment">// 打印: p</span>
</span><span class="code-line"><span class="token comment">// 打印: e</span>
</span><span class="code-line"><span class="token comment">// 打印: r</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="使用下划线"><a aria-hidden="true" tabindex="-1" href="#使用下划线"><span class="icon icon-link"></span></a>使用下划线</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">for</span> <span class="token omit keyword">_</span> <span class="token keyword">in</span> <span class="token number">1</span><span class="token operator">...</span><span class="token number">3</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"Olé"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 打印: Olé</span>
</span><span class="code-line"><span class="token comment">// 打印: Olé</span>
</span><span class="code-line"><span class="token comment">// 打印: Olé</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="while-循环"><a aria-hidden="true" tabindex="-1" href="#while-循环"><span class="icon icon-link"></span></a>while 循环</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> counter <span class="token operator">=</span> <span class="token number">1</span>
</span><span class="code-line"><span class="token keyword">var</span> stopNum <span class="token operator">=</span> <span class="token class-name">Int</span><span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token keyword">in</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token operator">...</span><span class="token number">10</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">while</span> counter <span class="token operator">&#x3C;</span> stopNum <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span>counter<span class="token punctuation">)</span>
</span><span class="code-line">  counter <span class="token operator">+=</span> <span class="token number">1</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 循环打印，直到满足停止条件</span>
</span></code></pre>
<p><code>while</code> 循环接受一个条件，并在所提供的条件为 <code>true</code> 时持续执行其主体代码。如果条件从不为假，则循环将继续运行，程序将陷入<code>无限循环</code></p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="数组和集合"><a aria-hidden="true" tabindex="-1" href="#数组和集合"><span class="icon icon-link"></span></a>数组和集合</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="array-数组"><a aria-hidden="true" tabindex="-1" href="#array-数组"><span class="icon icon-link"></span></a>Array 数组</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> scores <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token class-name">Int</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 数组为空：[]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="count-属性"><a aria-hidden="true" tabindex="-1" href="#count-属性"><span class="icon icon-link"></span></a>.count 属性</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> grocery <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"🥓"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"🥞"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"🍪"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"🥛"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"🍊"</span></span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>grocery<span class="token punctuation">.</span>count<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 打印: 5</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="索引"><a aria-hidden="true" tabindex="-1" href="#索引"><span class="icon icon-link"></span></a>索引</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>索引是指项目在有序列表中的位置，使用下标语法 <code>array[index]</code> 从数组中检索单个元素。</p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> vowels <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"a"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"e"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"i"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"o"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"u"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>vowels<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment">// 打印: a</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>vowels<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment">// 打印: e</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>vowels<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment">// 打印: i</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>vowels<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment">// 打印: o</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>vowels<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span><span class="token punctuation">)</span>  <span class="token comment">// 打印: u</span>
</span></code></pre>
<p>注意：Swift 数组是零索引的，这意味着第一个元素的索引为 0。</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="用数组字面量初始化"><a aria-hidden="true" tabindex="-1" href="#用数组字面量初始化"><span class="icon icon-link"></span></a>用数组字面量初始化</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token comment">// 使用类型推断：</span>
</span><span class="code-line"><span class="token keyword">var</span> snowfall <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2.4</span><span class="token punctuation">,</span> <span class="token number">3.6</span><span class="token punctuation">,</span> <span class="token number">3.4</span><span class="token punctuation">,</span> <span class="token number">1.8</span><span class="token punctuation">,</span> <span class="token number">0.0</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment">// 明确类型：</span>
</span><span class="code-line"><span class="token keyword">var</span> temp<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token class-name">Int</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">33</span><span class="token punctuation">,</span> <span class="token number">31</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">,</span> <span class="token number">38</span><span class="token punctuation">,</span> <span class="token number">44</span><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="append-方法和--运算符"><a aria-hidden="true" tabindex="-1" href="#append-方法和--运算符"><span class="icon icon-link"></span></a>.append() 方法和 += 运算符</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> gymBadges <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"Boulder"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Cascade"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">gymBadges<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"Thunder"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">gymBadges <span class="token operator">+=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"Rainbow"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Soul"</span></span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment">// ["Boulder", "Cascade", "Thunder",</span>
</span><span class="code-line"><span class="token comment">//   "Rainbow", "Soul"]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="insert-和-remove-方法"><a aria-hidden="true" tabindex="-1" href="#insert-和-remove-方法"><span class="icon icon-link"></span></a>.insert() 和 .remove() 方法</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> moon <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"🌖"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"🌗"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"🌘"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"🌑"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">moon<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"🌕"</span></span><span class="token punctuation">,</span> at<span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// ["🌕", "🌖", "🌗", "🌘", "🌑"]</span>
</span><span class="code-line">
</span><span class="code-line">moon<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>at<span class="token punctuation">:</span> <span class="token number">4</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// ["🌕", "🌖", "🌗", "🌘"]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="遍历数组"><a aria-hidden="true" tabindex="-1" href="#遍历数组"><span class="icon icon-link"></span></a>遍历数组</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> employees <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"小王"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"张三"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"王五"</span></span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token keyword">for</span> person <span class="token keyword">in</span> employees <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span>person<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 打印: 小王</span>
</span><span class="code-line"><span class="token comment">// 打印: 张三</span>
</span><span class="code-line"><span class="token comment">// 打印: 王五</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="集合set"><a aria-hidden="true" tabindex="-1" href="#集合set"><span class="icon icon-link"></span></a>集合(Set)</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> paintingsInMOMA<span class="token punctuation">:</span> <span class="token class-name">Set</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"The Dream"</span></span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"The Starry Night"</span></span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"The False Mirror"</span></span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span></code></pre>
<p>我们可以使用集合(<code>Set</code>)来存储相同数据类型的<code>唯一</code>元素</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="空集合set"><a aria-hidden="true" tabindex="-1" href="#空集合set"><span class="icon icon-link"></span></a>空集合(Set)</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> team <span class="token operator">=</span> <span class="token class-name">Set</span><span class="token operator">&#x3C;</span><span class="token class-name">String</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>team<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 打印: [] </span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="填充集合"><a aria-hidden="true" tabindex="-1" href="#填充集合"><span class="icon icon-link"></span></a>填充集合</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> vowels<span class="token punctuation">:</span> <span class="token class-name">Set</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"a"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"e"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"i"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"o"</span></span><span class="token punctuation">,</span><span class="token string-literal"><span class="token string">"u"</span></span><span class="token punctuation">]</span>
</span></code></pre>
<p>要创建一个填充有值的集合，请在赋值运算符之前使用 <code>Set</code> 关键字。</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="insert"><a aria-hidden="true" tabindex="-1" href="#insert"><span class="icon icon-link"></span></a>.insert()</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> cookieJar<span class="token punctuation">:</span> <span class="token class-name">Set</span> <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Chocolate Chip"</span></span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Oatmeal Raisin"</span></span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment">// 添加一个新元素</span>
</span><span class="code-line">cookieJar<span class="token punctuation">.</span><span class="token function">insert</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"Peanut Butter Chip"</span></span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="remove-和-removeall-方法"><a aria-hidden="true" tabindex="-1" href="#remove-和-removeall-方法"><span class="icon icon-link"></span></a>.remove() 和 .removeAll() 方法</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> oddNumbers<span class="token punctuation">:</span> <span class="token class-name">Set</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 移除现有元素</span>
</span><span class="code-line">oddNumbers<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 删除所有元素</span>
</span><span class="code-line">oddNumbers<span class="token punctuation">.</span><span class="token function">removeAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="contains"><a aria-hidden="true" tabindex="-1" href="#contains"><span class="icon icon-link"></span></a>.contains()</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> names<span class="token punctuation">:</span> <span class="token class-name">Set</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"Rosa"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Doug"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Waldo"</span></span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>names<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"Lola"</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">//打印: false</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">if</span> names<span class="token punctuation">.</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"Waldo"</span></span><span class="token punctuation">)</span><span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"There's Waldo!"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"Where's Waldo?"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 打印: There's Waldo!</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="迭代一个集合"><a aria-hidden="true" tabindex="-1" href="#迭代一个集合"><span class="icon icon-link"></span></a>迭代一个集合</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> recipe<span class="token punctuation">:</span> <span class="token class-name">Set</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"蛋"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"面粉"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"糖"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">for</span> ingredient <span class="token keyword">in</span> recipe <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span> <span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"在配方中包含</span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">ingredient</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="isempty-属性"><a aria-hidden="true" tabindex="-1" href="#isempty-属性"><span class="icon icon-link"></span></a>.isEmpty 属性</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> emptySet <span class="token operator">=</span> <span class="token class-name">Set</span><span class="token operator">&#x3C;</span><span class="token class-name">String</span><span class="token operator">></span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>emptySet<span class="token punctuation">.</span>isEmpty<span class="token punctuation">)</span>  <span class="token comment">// 打印: true</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">var</span> populatedSet<span class="token punctuation">:</span> <span class="token class-name">Set</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>populatedSet<span class="token punctuation">.</span>isEmpty<span class="token punctuation">)</span> <span class="token comment">// 打印: false</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="count-属性-1"><a aria-hidden="true" tabindex="-1" href="#count-属性-1"><span class="icon icon-link"></span></a>.count 属性</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> band<span class="token punctuation">:</span> <span class="token class-name">Set</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"张三"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"王五"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"赵六"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"乐队有 </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">band<span class="token punctuation">.</span>count</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string"> 名演奏者。"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 打印: 乐队有 4 名演奏者。</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="intersection-交叉"><a aria-hidden="true" tabindex="-1" href="#intersection-交叉"><span class="icon icon-link"></span></a>.intersection() 交叉</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> setA<span class="token punctuation">:</span> <span class="token class-name">Set</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"A"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"B"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"C"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"D"</span></span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token keyword">var</span> setB<span class="token punctuation">:</span> <span class="token class-name">Set</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"C"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"D"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"E"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"F"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">var</span> setC <span class="token operator">=</span> setA<span class="token punctuation">.</span><span class="token function">intersection</span><span class="token punctuation">(</span>setB<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>setC<span class="token punctuation">)</span>  <span class="token comment">// 打印: ["D", "C"]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="union-合并去重"><a aria-hidden="true" tabindex="-1" href="#union-合并去重"><span class="icon icon-link"></span></a>.union() 合并去重</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> setA<span class="token punctuation">:</span> <span class="token class-name">Set</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"A"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"B"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"C"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"D"</span></span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token keyword">var</span> setB<span class="token punctuation">:</span> <span class="token class-name">Set</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"C"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"D"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"E"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"F"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">var</span> setC <span class="token operator">=</span> setA<span class="token punctuation">.</span><span class="token function">union</span><span class="token punctuation">(</span>setB<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>setC<span class="token punctuation">)</span> 
</span><span class="code-line"><span class="token comment">// 打印: ["B", "A", "D", "F", "C", "E"]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="symmetricdifference-对称差"><a aria-hidden="true" tabindex="-1" href="#symmetricdifference-对称差"><span class="icon icon-link"></span></a>.symmetricDifference() 对称差</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> setA<span class="token punctuation">:</span> <span class="token class-name">Set</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"A"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"B"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"C"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"D"</span></span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token keyword">var</span> setB<span class="token punctuation">:</span> <span class="token class-name">Set</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"C"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"D"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"E"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"F"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">var</span> setC <span class="token operator">=</span> setA<span class="token punctuation">.</span><span class="token function">symmetricDifference</span><span class="token punctuation">(</span>setB<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>setC<span class="token punctuation">)</span> 
</span><span class="code-line"><span class="token comment">// 打印: ["B", "E", "F", "A"]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="subtracting-减法"><a aria-hidden="true" tabindex="-1" href="#subtracting-减法"><span class="icon icon-link"></span></a>.subtracting() 减法</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> setA<span class="token punctuation">:</span> <span class="token class-name">Set</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"A"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"B"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"C"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"D"</span></span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token keyword">var</span> setB<span class="token punctuation">:</span> <span class="token class-name">Set</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"C"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"D"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">var</span> setC <span class="token operator">=</span> setA<span class="token punctuation">.</span><span class="token function">subtracting</span><span class="token punctuation">(</span>setB<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>setC<span class="token punctuation">)</span> 
</span><span class="code-line"><span class="token comment">// 打印: ["B", "A"]</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="字典"><a aria-hidden="true" tabindex="-1" href="#字典"><span class="icon icon-link"></span></a>字典</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="基础字典"><a aria-hidden="true" tabindex="-1" href="#基础字典"><span class="icon icon-link"></span></a>基础字典</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> dictionaryName <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Key1"</span></span><span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"Value1"</span></span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Key2"</span></span><span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"Value2"</span></span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Key3"</span></span><span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"Value3"</span></span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span></code></pre>
<p>成对数据或键值对的<code>无序</code>集合</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="keys"><a aria-hidden="true" tabindex="-1" href="#keys"><span class="icon icon-link"></span></a>Keys</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> fruitStand <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Coconuts"</span></span><span class="token punctuation">:</span> <span class="token number">12</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Pineapples"</span></span><span class="token punctuation">:</span> <span class="token number">12</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Papaya"</span></span><span class="token punctuation">:</span> <span class="token number">12</span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span></code></pre>
<p>每个<code>键</code>都是<code>唯一</code>的，即使它们都包含相同的<code>值</code></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="类型一致性"><a aria-hidden="true" tabindex="-1" href="#类型一致性"><span class="icon icon-link"></span></a>类型一致性</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> numberOfSides <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"triangle"</span></span><span class="token punctuation">:</span> <span class="token number">3</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"square"</span></span><span class="token punctuation">:</span> <span class="token number">4</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"rectangle"</span></span><span class="token punctuation">:</span> <span class="token number">4</span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span></code></pre>
<p>仅包含 <code>String</code> 键和 <code>Int</code> 值</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="初始化填充字典"><a aria-hidden="true" tabindex="-1" href="#初始化填充字典"><span class="icon icon-link"></span></a>初始化填充字典</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> employeeID <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Hamlet"</span></span><span class="token punctuation">:</span> <span class="token number">1367</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Horatio"</span></span><span class="token punctuation">:</span> <span class="token number">8261</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Ophelia"</span></span><span class="token punctuation">:</span> <span class="token number">9318</span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="初始化一个空字典"><a aria-hidden="true" tabindex="-1" href="#初始化一个空字典"><span class="icon icon-link"></span></a>初始化一个空字典</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token comment">// 初始化器语法：</span>
</span><span class="code-line"><span class="token keyword">var</span> yearlyFishPopulation <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token class-name">Int</span><span class="token punctuation">:</span> <span class="token class-name">Int</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 空字典字面量语法：</span>
</span><span class="code-line"><span class="token keyword">var</span> yearlyBirdPopulation<span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token class-name">Int</span><span class="token punctuation">:</span> <span class="token class-name">Int</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="添加到字典"><a aria-hidden="true" tabindex="-1" href="#添加到字典"><span class="icon icon-link"></span></a>添加到字典</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> pronunciation <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"library"</span></span><span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"lai·breh·ree"</span></span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"apple"</span></span><span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"a·pl"</span></span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment">// 新键：“programming”，新值：“prow·gra”</span>
</span><span class="code-line">pronunciation<span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"programming"</span></span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"prow·gra"</span></span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="删除键值对"><a aria-hidden="true" tabindex="-1" href="#删除键值对"><span class="icon icon-link"></span></a>删除键值对</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> bookShelf <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Goodnight"</span></span><span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"Margaret Wise Brown"</span></span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"The BFG"</span></span><span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"Roald Dahl"</span></span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Falling Up"</span></span><span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"Shel Silverstein"</span></span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"No, David!"</span></span><span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"David Shannon"</span></span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 通过将 key 设置为 nil 来删除值</span>
</span><span class="code-line">bookShelf<span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"The BFG"</span></span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token nil constant">nil</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 使用 .removeValue() 删除值</span>
</span><span class="code-line">bookShelf<span class="token punctuation">.</span><span class="token function">removeValue</span><span class="token punctuation">(</span>forKey<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"Goodnight"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 删除所有值</span>
</span><span class="code-line">bookShelf<span class="token punctuation">.</span><span class="token function">removeAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="修改键值对"><a aria-hidden="true" tabindex="-1" href="#修改键值对"><span class="icon icon-link"></span></a>修改键值对</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> change <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Quarter"</span></span><span class="token punctuation">:</span> <span class="token number">0.29</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Dime"</span></span><span class="token punctuation">:</span> <span class="token number">0.15</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Nickel"</span></span><span class="token punctuation">:</span> <span class="token number">0.05</span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 使用下标语法更改值</span>
</span><span class="code-line">change<span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"Quarter"</span></span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">.</span><span class="token number">25</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 使用 .updateValue() 更改值</span>
</span><span class="code-line">change<span class="token punctuation">.</span><span class="token function">updateValue</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token number">10</span><span class="token punctuation">,</span> forKey<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"Dime"</span></span><span class="token punctuation">)</span>
</span></code></pre>
<p>要更改键值对的值，请使用 <code>.updateValue()</code> 方法或下标语法，通过将括号 <code>[ ]</code> 和其中的现有键附加到字典的名称，然后添加赋值运算符 <em>(<code>=</code>)</em> 后跟修改后的值</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="isempty-属性-1"><a aria-hidden="true" tabindex="-1" href="#isempty-属性-1"><span class="icon icon-link"></span></a>.isEmpty 属性</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> bakery <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token class-name">String</span><span class="token punctuation">:</span><span class="token class-name">Int</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 检查字典是否为空</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>bakery<span class="token punctuation">.</span>isEmpty<span class="token punctuation">)</span>    <span class="token comment">// 打印 true</span>
</span><span class="code-line">bakery<span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"Cupcakes"</span></span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token number">12</span>
</span><span class="code-line"><span class="token comment">// 检查字典是否为空</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>bakery<span class="token punctuation">.</span>isEmpty<span class="token punctuation">)</span>    <span class="token comment">// 打印 false</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="count-属性-2"><a aria-hidden="true" tabindex="-1" href="#count-属性-2"><span class="icon icon-link"></span></a>.count 属性</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> fruitStand <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Apples"</span></span><span class="token punctuation">:</span> <span class="token number">12</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"Oranges"</span></span><span class="token punctuation">,</span> <span class="token number">17</span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>fruitStand<span class="token punctuation">.</span>count<span class="token punctuation">)</span>  <span class="token comment">// 打印: 2</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="为变量赋值"><a aria-hidden="true" tabindex="-1" href="#为变量赋值"><span class="icon icon-link"></span></a>为变量赋值</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> hex <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"red"</span></span><span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"#ff0000"</span></span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"yellow"</span></span><span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"#ffff00"</span></span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"blue"</span></span><span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"#0000ff"</span></span><span class="token punctuation">,</span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"蓝色十六进制代码 </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">hex<span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"blue"</span></span><span class="token punctuation">]</span></span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 打印: 蓝色十六进制代码 Optional("#0000ff")</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">if</span> <span class="token keyword">let</span> redHex <span class="token operator">=</span> hex<span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"red"</span></span><span class="token punctuation">]</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"红色的十六进制代码 </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">redHex</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 打印: 红色的十六进制代码 #ff0000</span>
</span></code></pre>
<p>将键值对的值分配给变量将返回一个可选值。要提取值，请使用可选的展开</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="遍历字典"><a aria-hidden="true" tabindex="-1" href="#遍历字典"><span class="icon icon-link"></span></a>遍历字典</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> emojiMeaning <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"🤔"</span></span><span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"Thinking Face"</span></span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"😪"</span></span><span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"Sleepy Face"</span></span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"😵"</span></span><span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"Dizzy Face"</span></span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token comment">// 遍历键和值</span>
</span><span class="code-line"><span class="token keyword">for</span> <span class="token punctuation">(</span>emoji<span class="token punctuation">,</span> meaning<span class="token punctuation">)</span> <span class="token keyword">in</span> emojiMeaning <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"</span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">emoji</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">被称为'</span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">meaning</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">Emoji'"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 仅通过键迭代</span>
</span><span class="code-line"><span class="token keyword">for</span> emoji <span class="token keyword">in</span> emojiMeaning<span class="token punctuation">.</span>keys <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span>emoji<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 仅通过值迭代</span>
</span><span class="code-line"><span class="token keyword">for</span> meaning <span class="token keyword">in</span> emojiMeaning<span class="token punctuation">.</span>values <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span>meaning<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="函数"><a aria-hidden="true" tabindex="-1" href="#函数"><span class="icon icon-link"></span></a>函数</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="基础函数"><a aria-hidden="true" tabindex="-1" href="#基础函数"><span class="icon icon-link"></span></a>基础函数</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">func</span> <span class="token function-definition function">washCar</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-></span> <span class="token class-name">Void</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"Soap"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"Scrub"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"Rinse"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"Dry"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="调用函数"><a aria-hidden="true" tabindex="-1" href="#调用函数"><span class="icon icon-link"></span></a>调用函数</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">func</span> <span class="token function-definition function">greetLearner</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line"> <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"欢迎来到 Quick Reference!"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 函数调用：</span>
</span><span class="code-line"><span class="token function">greetLearner</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 打印: 欢迎来到 Quick Reference!</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="返回值"><a aria-hidden="true" tabindex="-1" href="#返回值"><span class="icon icon-link"></span></a>返回值</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">let</span> birthYear <span class="token operator">=</span> <span class="token number">1994</span>
</span><span class="code-line"><span class="token keyword">var</span> currentYear <span class="token operator">=</span> <span class="token number">2020</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">func</span> <span class="token function-definition function">findAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-></span> <span class="token class-name">Int</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">return</span> currentYear <span class="token operator">-</span> birthYear
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token function">findAge</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// 打印: 26</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="多个参数"><a aria-hidden="true" tabindex="-1" href="#多个参数"><span class="icon icon-link"></span></a>多个参数</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">func</span> <span class="token function-definition function">convertFracToDec</span><span class="token punctuation">(</span>numerator<span class="token punctuation">:</span> <span class="token class-name">Double</span><span class="token punctuation">,</span> denominator<span class="token punctuation">:</span> <span class="token class-name">Double</span><span class="token punctuation">)</span> <span class="token operator">-></span> <span class="token class-name">Double</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">return</span> numerator <span class="token operator">/</span> denominator
</span><span class="code-line"><span class="token punctuation">}</span> 
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">let</span> decimal <span class="token operator">=</span> <span class="token function">convertFracToDec</span><span class="token punctuation">(</span>numerator<span class="token punctuation">:</span> <span class="token number">1.0</span><span class="token punctuation">,</span> denominator<span class="token punctuation">:</span> <span class="token number">2.0</span><span class="token punctuation">)</span> 
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>decimal<span class="token punctuation">)</span> <span class="token comment">// Prints:  0.5 </span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="省略参数标签"><a aria-hidden="true" tabindex="-1" href="#省略参数标签"><span class="icon icon-link"></span></a>省略参数标签</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">func</span> <span class="token function-definition function">findDiff</span><span class="token punctuation">(</span><span class="token omit keyword">_</span> a<span class="token punctuation">:</span> <span class="token class-name">Int</span><span class="token punctuation">,</span> b<span class="token punctuation">:</span> <span class="token class-name">Int</span><span class="token punctuation">)</span> <span class="token operator">-></span> <span class="token class-name">Int</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">return</span> a <span class="token operator">-</span> b
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token function">findDiff</span><span class="token punctuation">(</span><span class="token number">6</span><span class="token punctuation">,</span> b<span class="token punctuation">:</span> <span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// 打印: 2</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="返回多个值"><a aria-hidden="true" tabindex="-1" href="#返回多个值"><span class="icon icon-link"></span></a>返回多个值</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">func</span> <span class="token function-definition function">smartphoneModel</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-></span> <span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token punctuation">,</span> version<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token punctuation">,</span> yearReleased<span class="token punctuation">:</span> <span class="token class-name">Int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"iPhone"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"8 Plus"</span></span><span class="token punctuation">,</span> <span class="token number">2017</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">let</span> phone <span class="token operator">=</span> <span class="token function">smartphoneModel</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>phone<span class="token punctuation">.</span>name<span class="token punctuation">)</span>     <span class="token comment">// 打印: iPhone</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>phone<span class="token punctuation">.</span>version<span class="token punctuation">)</span>  <span class="token comment">// 打印: 8 Plus</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>phone<span class="token punctuation">.</span>yearReleased<span class="token punctuation">)</span> <span class="token comment">// 打印: 2017</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="parameters--arguments"><a aria-hidden="true" tabindex="-1" href="#parameters--arguments"><span class="icon icon-link"></span></a>Parameters &#x26; Arguments</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">func</span> <span class="token function-definition function">findSquarePerimet</span><span class="token punctuation">(</span>side<span class="token punctuation">:</span> <span class="token class-name">Int</span><span class="token punctuation">)</span> <span class="token operator">-></span> <span class="token class-name">Int</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">return</span> side <span class="token operator">*</span> <span class="token number">4</span>
</span><span class="code-line"><span class="token punctuation">}</span> 
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">let</span> perimeter <span class="token operator">=</span> <span class="token function">findSquarePerimet</span><span class="token punctuation">(</span>side<span class="token punctuation">:</span> <span class="token number">5</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>perimeter<span class="token punctuation">)</span> <span class="token comment">// 打印: 20</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Parameter: side</span>
</span><span class="code-line"><span class="token comment">// Argument:  5</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="隐式返回"><a aria-hidden="true" tabindex="-1" href="#隐式返回"><span class="icon icon-link"></span></a>隐式返回</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">func</span> <span class="token function-definition function">nextTotalSolarEclipse</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-></span> <span class="token class-name">String</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token string-literal"><span class="token string">"April 8th, 2024 🌎"</span></span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token function">nextTotalSolarEclipse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 打印: April 8th, 2024 🌎</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="默认参数"><a aria-hidden="true" tabindex="-1" href="#默认参数"><span class="icon icon-link"></span></a>默认参数</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">func</span> <span class="token function-definition function">greet</span><span class="token punctuation">(</span>person<span class="token punctuation">:</span> <span class="token class-name">String</span> <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"guest"</span></span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"Hello </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">person</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token function">greet</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// Hello guest</span>
</span><span class="code-line"><span class="token function">greet</span><span class="token punctuation">(</span>person<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"Aliya"</span></span><span class="token punctuation">)</span> <span class="token comment">// Hello Aliya</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="输入输出参数"><a aria-hidden="true" tabindex="-1" href="#输入输出参数"><span class="icon icon-link"></span></a>输入输出参数</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> currentSeason <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"冬天"</span></span> 
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">func</span> <span class="token function-definition function">season</span><span class="token punctuation">(</span>month<span class="token punctuation">:</span> <span class="token class-name">Int</span><span class="token punctuation">,</span> name<span class="token punctuation">:</span> <span class="token keyword">inout</span> <span class="token class-name">String</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">switch</span> month <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">case</span> <span class="token number">1</span><span class="token operator">...</span><span class="token number">2</span><span class="token punctuation">:</span>
</span><span class="code-line">      name <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"冬天 ⛄️"</span></span> 
</span><span class="code-line">    <span class="token keyword">case</span> <span class="token number">3</span><span class="token operator">...</span><span class="token number">6</span><span class="token punctuation">:</span>
</span><span class="code-line">      name <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"春天 🌱"</span></span>
</span><span class="code-line">    <span class="token keyword">case</span> <span class="token number">7</span><span class="token operator">...</span><span class="token number">9</span><span class="token punctuation">:</span>
</span><span class="code-line">      name <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"夏天 ⛱"</span></span>
</span><span class="code-line">    <span class="token keyword">case</span> <span class="token number">10</span><span class="token operator">...</span><span class="token number">11</span><span class="token punctuation">:</span> 
</span><span class="code-line">      name <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"秋天 🍂"</span></span>
</span><span class="code-line">    <span class="token keyword">default</span><span class="token punctuation">:</span> 
</span><span class="code-line">      name <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"未知"</span></span>
</span><span class="code-line">  <span class="token punctuation">}</span> 
</span><span class="code-line"><span class="token punctuation">}</span> 
</span><span class="code-line">
</span><span class="code-line"><span class="token function">season</span><span class="token punctuation">(</span>monthNum<span class="token punctuation">:</span> <span class="token number">4</span><span class="token punctuation">,</span> name<span class="token punctuation">:</span> <span class="token operator">&#x26;</span>currentSeason<span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>currentSeason<span class="token punctuation">)</span> <span class="token comment">// 春天 🌱</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="可变参数"><a aria-hidden="true" tabindex="-1" href="#可变参数"><span class="icon icon-link"></span></a>可变参数</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">func</span> <span class="token function-definition function">totalStudent</span><span class="token punctuation">(</span>data<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token operator">...</span><span class="token punctuation">)</span> <span class="token operator">-></span> <span class="token class-name">Int</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">let</span> numStudents <span class="token operator">=</span> data<span class="token punctuation">.</span>count
</span><span class="code-line">  <span class="token keyword">return</span> numStudents
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token function">totalStudent</span><span class="token punctuation">(</span>data<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"王五"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"张三"</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 打印: 2</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="可选参数"><a aria-hidden="true" tabindex="-1" href="#可选参数"><span class="icon icon-link"></span></a>可选参数</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">func</span> <span class="token function-definition function">getFirstInitial</span><span class="token punctuation">(</span>from name<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token operator">?</span><span class="token punctuation">)</span> <span class="token operator">-></span> <span class="token class-name">String</span><span class="token operator">?</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">return</span> name<span class="token operator">?</span><span class="token punctuation">.</span>first
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<p>函数可以接受可选类型并返回可选类型。当一个函数不能返回请求类型的合理实例时，它应该返回 <code>nil</code></p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="结构"><a aria-hidden="true" tabindex="-1" href="#结构"><span class="icon icon-link"></span></a>结构</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="结构创建"><a aria-hidden="true" tabindex="-1" href="#结构创建"><span class="icon icon-link"></span></a>结构创建</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">struct</span> <span class="token class-name">Building</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">var</span> address<span class="token punctuation">:</span> <span class="token class-name">String</span>
</span><span class="code-line">  <span class="token keyword">var</span> floors<span class="token punctuation">:</span> <span class="token class-name">Int</span>
</span><span class="code-line">
</span><span class="code-line">  <span class="token keyword">init</span><span class="token punctuation">(</span>address<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token punctuation">,</span> floors<span class="token punctuation">:</span> <span class="token class-name">Int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">self</span><span class="token punctuation">.</span>address <span class="token operator">=</span> address
</span><span class="code-line">    <span class="token keyword">self</span><span class="token punctuation">.</span>floors <span class="token operator">=</span> floors
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<p>结构或结构用于以编程方式在代码中表示现实生活中的对象。结构是使用 <code>struct</code> 关键字创建的，后跟其名称，然后是包含其属性和方法的主体</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="默认属性值"><a aria-hidden="true" tabindex="-1" href="#默认属性值"><span class="icon icon-link"></span></a>默认属性值</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">struct</span> <span class="token class-name">Car</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">var</span> numOfWheels <span class="token operator">=</span> <span class="token number">4</span>
</span><span class="code-line">  <span class="token keyword">var</span> topSpeed <span class="token operator">=</span> <span class="token number">80</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">var</span> reliantRobin <span class="token operator">=</span> <span class="token class-name">Car</span><span class="token punctuation">(</span>numOfWheels<span class="token punctuation">:</span> <span class="token number">3</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>reliantRobin<span class="token punctuation">.</span>numOfWheels<span class="token punctuation">)</span> <span class="token comment">// 打印: 3</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>reliantRobin<span class="token punctuation">.</span>topSpeed<span class="token punctuation">)</span>    <span class="token comment">// 打印: 80</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="结构实例创建"><a aria-hidden="true" tabindex="-1" href="#结构实例创建"><span class="icon icon-link"></span></a>结构实例创建</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">struct</span> <span class="token class-name">Person</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">var</span> name<span class="token punctuation">:</span> <span class="token class-name">String</span>
</span><span class="code-line">  <span class="token keyword">var</span> age<span class="token punctuation">:</span> <span class="token class-name">Int</span>
</span><span class="code-line">
</span><span class="code-line">  <span class="token keyword">init</span><span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token punctuation">,</span> age<span class="token punctuation">:</span> <span class="token class-name">Int</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">self</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name
</span><span class="code-line">    <span class="token keyword">self</span><span class="token punctuation">.</span>age <span class="token operator">=</span> age
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Person 实例：</span>
</span><span class="code-line"><span class="token keyword">var</span> morty <span class="token operator">=</span> <span class="token class-name">Person</span><span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"张三"</span></span><span class="token punctuation">,</span> age<span class="token punctuation">:</span> <span class="token number">14</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="init-方法"><a aria-hidden="true" tabindex="-1" href="#init-方法"><span class="icon icon-link"></span></a>init() 方法</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">struct</span> <span class="token constant">TV</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">var</span> size<span class="token punctuation">:</span> <span class="token class-name">Int</span>
</span><span class="code-line">  <span class="token keyword">var</span> type<span class="token punctuation">:</span> <span class="token class-name">String</span>
</span><span class="code-line">  
</span><span class="code-line">  <span class="token keyword">init</span><span class="token punctuation">(</span>size<span class="token punctuation">:</span> <span class="token class-name">Int</span><span class="token punctuation">,</span> type<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">self</span><span class="token punctuation">.</span>size <span class="token operator">=</span> size
</span><span class="code-line">    <span class="token keyword">self</span><span class="token punctuation">.</span>type <span class="token operator">=</span> type
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<p>使用 <code>TV</code> 类</p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> newTV <span class="token operator">=</span> <span class="token function">TV</span><span class="token punctuation">(</span>size<span class="token punctuation">:</span> <span class="token number">65</span><span class="token punctuation">,</span> type<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"LED"</span></span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="检查类型"><a aria-hidden="true" tabindex="-1" href="#检查类型"><span class="icon icon-link"></span></a>检查类型</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token function">type</span><span class="token punctuation">(</span>of<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"abc"</span></span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">// 打印: String</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token function">type</span><span class="token punctuation">(</span>of<span class="token punctuation">:</span> <span class="token number">123</span><span class="token punctuation">)</span><span class="token punctuation">)</span>   <span class="token comment">// 打印: 123</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="变异方法mutating"><a aria-hidden="true" tabindex="-1" href="#变异方法mutating"><span class="icon icon-link"></span></a>变异方法(mutating)</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">struct</span> <span class="token class-name">Menu</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">var</span> menuItems <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string-literal"><span class="token string">"Fries"</span></span><span class="token punctuation">,</span> <span class="token string-literal"><span class="token string">"Burgers"</span></span><span class="token punctuation">]</span>
</span><span class="code-line">  <span class="token keyword">mutating</span> <span class="token keyword">func</span> <span class="token function-definition function">addToMenu</span><span class="token punctuation">(</span>dish<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">self</span><span class="token punctuation">.</span>menuItems<span class="token punctuation">.</span><span class="token function">append</span><span class="token punctuation">(</span>dish<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<p>使用 <code>Menu</code> 类</p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> dinerMenu <span class="token operator">=</span> <span class="token class-name">Menu</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">dinerMenu<span class="token punctuation">.</span><span class="token function">addToMenu</span><span class="token punctuation">(</span>dish<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"Toast"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>dinerMenu<span class="token punctuation">.</span>menuItems<span class="token punctuation">)</span> 
</span><span class="code-line"><span class="token comment">// 打印: ["Fries", "Burgers", "Toast"]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="结构方法"><a aria-hidden="true" tabindex="-1" href="#结构方法"><span class="icon icon-link"></span></a>结构方法</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">struct</span> <span class="token class-name">Dog</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">func</span> <span class="token function-definition function">bark</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"Woof"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token keyword">let</span> fido <span class="token operator">=</span> <span class="token class-name">Dog</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">fido<span class="token punctuation">.</span><span class="token function">bark</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">// 打印: Woof</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="class"><a aria-hidden="true" tabindex="-1" href="#class"><span class="icon icon-link"></span></a>Class</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="引用类型类"><a aria-hidden="true" tabindex="-1" href="#引用类型类"><span class="icon icon-link"></span></a>引用类型（类）</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Player</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">var</span> name<span class="token punctuation">:</span> <span class="token class-name">String</span>
</span><span class="code-line">
</span><span class="code-line">  <span class="token keyword">init</span><span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">self</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">var</span> player1 <span class="token operator">=</span> <span class="token class-name">Player</span><span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"Tomoko"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">var</span> player2 <span class="token operator">=</span> player1
</span><span class="code-line">player2<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Isabella"</span></span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>player1<span class="token punctuation">.</span>name<span class="token punctuation">)</span> <span class="token comment">// Isabella</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>player2<span class="token punctuation">.</span>name<span class="token punctuation">)</span> <span class="token comment">// Isabella</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="类的实例"><a aria-hidden="true" tabindex="-1" href="#类的实例"><span class="icon icon-link"></span></a>类的实例</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Person</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">var</span> name <span class="token operator">=</span> <span class="token string-literal"><span class="token string">""</span></span>
</span><span class="code-line">  <span class="token keyword">var</span> age <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">var</span> sonny <span class="token operator">=</span> <span class="token class-name">Person</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// sonny 现在是 Person 的一个实例</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="init-方法-1"><a aria-hidden="true" tabindex="-1" href="#init-方法-1"><span class="icon icon-link"></span></a>init() 方法</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Fruit</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">var</span> hasSeeds <span class="token operator">=</span> <span class="token boolean">true</span> 
</span><span class="code-line">  <span class="token keyword">var</span> color<span class="token punctuation">:</span> <span class="token class-name">String</span>
</span><span class="code-line">
</span><span class="code-line">  <span class="token keyword">init</span><span class="token punctuation">(</span>color<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">self</span><span class="token punctuation">.</span>color <span class="token operator">=</span> color
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<p>使用 Fruit 类</p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">let</span> apple <span class="token operator">=</span> <span class="token class-name">Fruit</span><span class="token punctuation">(</span>color<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"red"</span></span><span class="token punctuation">)</span>
</span></code></pre>
<p>可以使用 <code>init()</code> 方法和相应的初始化属性来初始化类，在 <code>init()</code> 方法中，<code>self</code> 关键字用于引用类分配属性值的实际实例</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="类属性"><a aria-hidden="true" tabindex="-1" href="#类属性"><span class="icon icon-link"></span></a>类属性</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">var</span> ferris <span class="token operator">=</span> <span class="token class-name">Student</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">ferris<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Ferris Bueller"</span></span>
</span><span class="code-line">ferris<span class="token punctuation">.</span>year <span class="token operator">=</span> <span class="token number">12</span>
</span><span class="code-line">ferris<span class="token punctuation">.</span>gpa <span class="token operator">=</span> <span class="token number">3.81</span>
</span><span class="code-line">ferris<span class="token punctuation">.</span>honors <span class="token operator">=</span> <span class="token boolean">false</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="继承"><a aria-hidden="true" tabindex="-1" href="#继承"><span class="icon icon-link"></span></a>继承</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>假设我们有一个 BankAccount 类：</p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">BankAccount</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">var</span> balance <span class="token operator">=</span> <span class="token number">0.0</span>
</span><span class="code-line">  <span class="token keyword">func</span> <span class="token function-definition function">deposit</span><span class="token punctuation">(</span>amount<span class="token punctuation">:</span> <span class="token class-name">Double</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    balance <span class="token operator">+=</span> amount
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line">  <span class="token keyword">func</span> <span class="token function-definition function">withdraw</span><span class="token punctuation">(</span>amount<span class="token punctuation">:</span> <span class="token class-name">Double</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    balance <span class="token operator">-=</span> amount
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<p><code>SavingsAccount</code> 继承 <code>BankAccount</code> 类</p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">SavingsAccount</span><span class="token punctuation">:</span> <span class="token class-name">BankAccount</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">var</span> interest <span class="token operator">=</span> <span class="token number">0.0</span>
</span><span class="code-line">
</span><span class="code-line">  <span class="token keyword">func</span> <span class="token function-definition function">addInterest</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">let</span> interest <span class="token operator">=</span> balance <span class="token operator">*</span> <span class="token number">0.005</span>
</span><span class="code-line">    <span class="token keyword">self</span><span class="token punctuation">.</span><span class="token function">deposit</span><span class="token punctuation">(</span>amount<span class="token punctuation">:</span> interest<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<p>新的 <code>SavingsAccount</code> 类(子类)自动获得了 <code>BankAccount</code> 类(超类)的所有特征。 此外，<code>SavingsAccount</code> 类定义了一个 <code>.interest</code> 属性和一个 <code>.addInterest()</code> 方法。</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="示例"><a aria-hidden="true" tabindex="-1" href="#示例"><span class="icon icon-link"></span></a>示例</h3><div class="wrap-body">
<p>使用数据类型</p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">var</span> name<span class="token punctuation">:</span> <span class="token class-name">String</span>
</span><span class="code-line">  <span class="token keyword">var</span> year<span class="token punctuation">:</span> <span class="token class-name">Int</span>
</span><span class="code-line">  <span class="token keyword">var</span> gpa<span class="token punctuation">:</span> <span class="token class-name">Double</span>
</span><span class="code-line">  <span class="token keyword">var</span> honors<span class="token punctuation">:</span> <span class="token class-name">Bool</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<p>使用默认属性值</p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">Student</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">var</span> name <span class="token operator">=</span> <span class="token string-literal"><span class="token string">""</span></span>
</span><span class="code-line">  <span class="token keyword">var</span> gpa <span class="token operator">=</span> <span class="token number">0.0</span>
</span><span class="code-line">  <span class="token keyword">var</span> honors <span class="token operator">=</span> <span class="token boolean">false</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="这是结构定义和类定义的示例"><a aria-hidden="true" tabindex="-1" href="#这是结构定义和类定义的示例"><span class="icon icon-link"></span></a>这是结构定义和类定义的示例</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">struct</span> <span class="token class-name">Resolution</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">var</span> width <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line">  <span class="token keyword">var</span> height <span class="token operator">=</span> <span class="token number">0</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token keyword">class</span> <span class="token class-name">VideoMode</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">var</span> resolution <span class="token operator">=</span> <span class="token class-name">Resolution</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">var</span> interlaced <span class="token operator">=</span> <span class="token boolean">false</span>
</span><span class="code-line">  <span class="token keyword">var</span> frameRate <span class="token operator">=</span> <span class="token number">0.0</span>
</span><span class="code-line">  <span class="token keyword">var</span> name<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token operator">?</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<p><code>Resolution</code> 结构定义和 <code>VideoMode</code> 类定义仅描述 <code>Resolution</code> 或 <code>VideoMode</code> 的外观，创建结构或类的实例：</p>
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">let</span> resolution <span class="token operator">=</span> <span class="token class-name">Resolution</span><span class="token punctuation">(</span>width<span class="token punctuation">:</span> <span class="token number">1920</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">let</span> someVideoMode <span class="token operator">=</span> <span class="token class-name">VideoMode</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="枚举"><a aria-hidden="true" tabindex="-1" href="#枚举"><span class="icon icon-link"></span></a>枚举</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="定义枚举"><a aria-hidden="true" tabindex="-1" href="#定义枚举"><span class="icon icon-link"></span></a>定义枚举</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">enum</span> <span class="token class-name">Day</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">case</span> monday
</span><span class="code-line">  <span class="token keyword">case</span> tuesday
</span><span class="code-line">  <span class="token keyword">case</span> wednesday
</span><span class="code-line">  <span class="token keyword">case</span> thursday
</span><span class="code-line">  <span class="token keyword">case</span> friday
</span><span class="code-line">  <span class="token keyword">case</span> saturday
</span><span class="code-line">  <span class="token keyword">case</span> sunday
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">let</span> casualWorkday<span class="token punctuation">:</span> <span class="token class-name">Day</span> <span class="token operator">=</span> <span class="token punctuation">.</span>friday
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="switch-语句-1"><a aria-hidden="true" tabindex="-1" href="#switch-语句-1"><span class="icon icon-link"></span></a>Switch 语句</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">enum</span> <span class="token class-name">Dessert</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token function">cake</span><span class="token punctuation">(</span>flavor<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token function">vanillaIceCream</span><span class="token punctuation">(</span>scoops<span class="token punctuation">:</span> <span class="token class-name">Int</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">case</span> brownie
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">let</span> customerOrder<span class="token punctuation">:</span> <span class="token class-name">Dessert</span> <span class="token operator">=</span> <span class="token punctuation">.</span><span class="token function">cake</span><span class="token punctuation">(</span>flavor<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"红色天鹅绒"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">switch</span> customerOrder <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token keyword">let</span> <span class="token punctuation">.</span><span class="token function">cake</span><span class="token punctuation">(</span>flavor<span class="token punctuation">)</span><span class="token punctuation">:</span>
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"你点了一个 </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation">flavor</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string"> 蛋糕"</span></span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token punctuation">.</span>brownie<span class="token punctuation">:</span> 
</span><span class="code-line">    <span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"你点了一块巧克力蛋糕"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token comment">// 打印: "你点了一个红色天鹅绒蛋糕"</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="caseiterable"><a aria-hidden="true" tabindex="-1" href="#caseiterable"><span class="icon icon-link"></span></a>CaseIterable</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">enum</span> <span class="token class-name">Season</span><span class="token punctuation">:</span> <span class="token class-name">CaseIterable</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">case</span> winter
</span><span class="code-line">  <span class="token keyword">case</span> spring
</span><span class="code-line">  <span class="token keyword">case</span> summer
</span><span class="code-line">  <span class="token keyword">case</span> fall
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">for</span> season <span class="token keyword">in</span> <span class="token class-name">Season</span><span class="token punctuation">.</span>allCases <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">print</span><span class="token punctuation">(</span>season<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<p>添加对 <code>CaseIterable</code> 协议的一致性以访问 <code>allCases</code> 属性，该属性返回枚举的所有案例的数组</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="原始值"><a aria-hidden="true" tabindex="-1" href="#原始值"><span class="icon icon-link"></span></a>原始值</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">enum</span> <span class="token class-name">Beatle</span><span class="token punctuation">:</span> <span class="token class-name">String</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">case</span> john<span class="token punctuation">,</span> paul<span class="token punctuation">,</span> george<span class="token punctuation">,</span> ringo
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span><span class="token string-literal"><span class="token string">"披头士是 </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation"><span class="token class-name">Beatle</span><span class="token punctuation">.</span>john<span class="token punctuation">.</span>rawValue</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">."</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 打印: 披头士是 john.</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="相关值"><a aria-hidden="true" tabindex="-1" href="#相关值"><span class="icon icon-link"></span></a>相关值</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">enum</span> <span class="token class-name">Dessert</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token function">cake</span><span class="token punctuation">(</span>flavor<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">case</span> <span class="token function">vanillaIceCream</span><span class="token punctuation">(</span>scoops<span class="token punctuation">:</span> <span class="token class-name">Int</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword">case</span> brownie
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">let</span> order<span class="token punctuation">:</span> <span class="token class-name">Dessert</span> <span class="token operator">=</span> <span class="token punctuation">.</span><span class="token function">cake</span><span class="token punctuation">(</span>flavor<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"红色天鹅绒"</span></span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="实例方法"><a aria-hidden="true" tabindex="-1" href="#实例方法"><span class="icon icon-link"></span></a>实例方法</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">enum</span> <span class="token class-name">Traffic</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">case</span> light
</span><span class="code-line">  <span class="token keyword">case</span> heavy
</span><span class="code-line">
</span><span class="code-line">  <span class="token keyword">mutating</span> <span class="token keyword">func</span> <span class="token function-definition function">reportAccident</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">self</span> <span class="token operator">=</span> <span class="token punctuation">.</span>heavy
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">var</span> currentTraffic<span class="token punctuation">:</span> <span class="token class-name">Traffic</span> <span class="token operator">=</span> <span class="token punctuation">.</span>light
</span><span class="code-line">
</span><span class="code-line">currentTraffic<span class="token punctuation">.</span><span class="token function">reportAccident</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// currentTraffic 现在是 .heavy</span>
</span></code></pre>
<p>就像类和结构一样，枚举也可以有实例方法。如果实例方法改变了枚举的值，则需要将其标记为 <code>mutating</code></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="从原始值初始化"><a aria-hidden="true" tabindex="-1" href="#从原始值初始化"><span class="icon icon-link"></span></a>从原始值初始化</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">enum</span> <span class="token class-name">Hello</span><span class="token punctuation">:</span> <span class="token class-name">String</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">case</span> english <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"Hello"</span></span>
</span><span class="code-line">  <span class="token keyword">case</span> japanese <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"你好呀！"</span></span>
</span><span class="code-line">  <span class="token keyword">case</span> emoji <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"👋"</span></span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token keyword">let</span> hello1 <span class="token operator">=</span> <span class="token class-name">Hello</span><span class="token punctuation">(</span>rawValue<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"你好呀！"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">let</span> hello2 <span class="token operator">=</span> <span class="token class-name">Hello</span><span class="token punctuation">(</span>rawValue<span class="token punctuation">:</span> <span class="token string-literal"><span class="token string">"Привет"</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>hello1<span class="token punctuation">)</span> <span class="token comment">// Optional(Hello.japanese)</span>
</span><span class="code-line"><span class="token function">print</span><span class="token punctuation">(</span>hello2<span class="token punctuation">)</span> <span class="token comment">// nil</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="计算属性"><a aria-hidden="true" tabindex="-1" href="#计算属性"><span class="icon icon-link"></span></a>计算属性</h3><div class="wrap-body">
<pre class="language-swift"><code class="language-swift code-highlight"><span class="code-line"><span class="token keyword">enum</span> <span class="token class-name">ShirtSize</span><span class="token punctuation">:</span> <span class="token class-name">String</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">case</span> small <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"S"</span></span>
</span><span class="code-line">  <span class="token keyword">case</span> medium <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"M"</span></span>
</span><span class="code-line">  <span class="token keyword">case</span> large <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"L"</span></span>
</span><span class="code-line">  <span class="token keyword">case</span> extraLarge <span class="token operator">=</span> <span class="token string-literal"><span class="token string">"XL"</span></span>
</span><span class="code-line">  <span class="token keyword">var</span> description<span class="token punctuation">:</span> <span class="token class-name">String</span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">return</span> <span class="token string-literal"><span class="token string">"这件衬衫尺码是 </span><span class="token interpolation-punctuation punctuation">\(</span><span class="token interpolation"><span class="token keyword">self</span><span class="token punctuation">.</span>rawValue</span><span class="token interpolation-punctuation punctuation">)</span><span class="token string">"</span></span>
</span><span class="code-line">  <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-not-exist"><div class="wrap-header h2wrap"><h2 id="另见"><a aria-hidden="true" tabindex="-1" href="#另见"><span class="icon icon-link"></span></a>另见</h2><div class="wrap-body">
<ul>
<li><a href="https://www.swift.org/documentation/">Swift 文档(官方)</a> <em>(swift.or)</em></li>
<li><a href="https://docs.swift.org/swift-book/">快速编程语言(官方)</a> <em>(swift.or)</em></li>
<li><a href="https://www.codecademy.com/resources/cheatsheets/language/swift">Learn Swift</a> <em>(codecademy.com)</em></li>
<li><a href="https://swiftly.dev/">Swift 开发人员的一站式快速参考</a> <em>(swiftly.dev)</em></li>
<li><a href="https://jaywcjlove.github.io/swift-tutorial">Swift 入门教程、读书笔记</a> <em>(jaywcjlove.github.io)</em></li>
</ul>
</div></div><div class="h2wrap-body"></div></div></div><script src="https://giscus.app/client.js" data-repo="jaywcjlove/reference" data-repo-id="R_kgDOID2-Mw" data-category="Q&#x26;A" data-category-id="DIC_kwDOID2-M84CS5wo" data-mapping="pathname" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="bottom" data-theme="dark" data-lang="zh-CN" crossorigin="anonymous" async></script><div class="giscus"></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 Kenny Wang.</footer></footer><script src="../data.js?v=1.5.2" defer></script><script src="../js/fuse.min.js?v=1.5.2" defer></script><script src="../js/main.js?v=1.5.2" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
